import time

from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMessageBox

from common.common import Common
from data_manage.can_tool import CanTool
from data_manage.work_thread import WorkThread


class ShowTabPCan:
    def __init__(self, ui, ptr):
        self.ui = ui
        self.open = True
        self.recv = True
        self.can_bus = None
        self.thread = None
        self.ptr = ptr
        pass

    def init_ui(self):
        png = Common.get_resource_path() + "提示_pcan.png"
        self.ui.toolButton_6.setIcon(QIcon(png))
        png = Common.get_resource_path() + "硬件.png"
        self.ui.toolButton_7.setIcon(QIcon(png))
        png = Common.get_resource_path() + "通道 (2).png"
        self.ui.toolButton_8.setIcon(QIcon(png))
        png = Common.get_resource_path() + "baud.png"
        self.ui.toolButton_9.setIcon(QIcon(png))

        self.ui.comboBox_interface.addItems(["socketcan", "kvaser", "serial",
                                             "slcan", "ixxat", "pcan", "usb2can",
                                             "nican", "iscan", "neovi", "vector",
                                             "virtual", "canalystii", "systec"])
        self.ui.comboBox_interface.setCurrentIndex(5)
        self.ui.comboBox_channel.addItems(["vcan0", "PCAN_USBBUS1", "0"])
        self.ui.comboBox_channel.setCurrentIndex(1)
        self.ui.comboBox_bitrate.addItems(["250000", "500000"])
        self.ui.comboBox_bitrate.setCurrentIndex(1)
        self.ui.pushButton_init_recv.setEnabled(False)
        self.init_connect()
        pass

    def init_connect(self):
        self.ui.pushButton_init_init.clicked.connect(lambda: self.on_init_init_click("on_init_init_click"))
        self.ui.pushButton_init_recv.clicked.connect(lambda: self.on_init_recv_click("on_init_recv_click"))
        pass

    def on_init_init_click(self, info):
        print(info)
        interface = self.ui.comboBox_interface.currentText()
        channel = self.ui.comboBox_channel.currentText()
        bit_rate = int(self.ui.comboBox_bitrate.currentText())
        print(interface, channel, bit_rate)
        if self.open:
            self.can_bus = CanTool(interface, channel, bit_rate)
            if self.can_bus.get_init() is None:
                QMessageBox.warning(self.ptr, "Error", "别试了，can线没接", QMessageBox.Cancel)
                return
            else:
                Common().set_can_bus(self.can_bus)
                self.ui.pushButton_init_init.setText("关闭CAN")
                # self.can_bus.bus_recv()
                self.open = False
                self.ui.pushButton_init_recv.setEnabled(True)
        else:
            if self.can_bus is not None:
                self.can_bus.shutdown_bus()
            self.ui.pushButton_init_init.setText("初始化CAN")
            self.open = True
            self.ui.pushButton_init_recv.setEnabled(False)
        pass

    def on_init_recv_click(self, info):
        print(info)
        if self.recv:
            # begin incoming inspection
            self.can_bus.send_data(0x224, [0xA5, 0x01, 0x20, 0x01, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            # receive motor drive & increase
            self.can_bus.send_data(0x226, [0xA5, 0x01, 0x50, 0x01, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            # receive motor mower
            self.can_bus.send_data(0x228, [0xA5, 0x01, 0x70, 0x01, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            # receive sensor IMU & rain & ultrasonic
            self.can_bus.send_data(0x22A, [0xA5, 0x01, 0x90, 0x01, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            # receive sensor light & current & battery & key & lift
            self.can_bus.send_data(0x22C, [0xA5, 0x01, 0xA0, 0x01, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            self.ui.pushButton_init_recv.setText("停止接收")
            self.recv = False
            Common().set_thread_recv(True)
            self.start_thread()
        else:
            self.can_bus.send_data(0x226, [0xA5, 0x01, 0x50, 0x00, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            self.can_bus.send_data(0x228, [0xA5, 0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            self.can_bus.send_data(0x22A, [0xA5, 0x01, 0x90, 0x00, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            self.can_bus.send_data(0x22C, [0xA5, 0x01, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            self.can_bus.send_data(0x224, [0xA5, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0xA5])
            time.sleep(0.08)
            self.ui.pushButton_init_recv.setText("开始接收")
            self.recv = True
            Common().set_thread_recv(False)
        pass

    def start_thread(self):
        self.thread = WorkThread(self.can_bus)
        self.thread.signal_update.connect(Common().get_show_tab_window().update_recv_data)
        self.thread.start()

